//
//  ImageModel.swift
//  Mixin
//
//  Created by 赵国腾 on 2018/8/30.
//  Copyright © 2018年 Insight. All rights reserved.
//

import UIKit

struct DataModel: Codable {
    
    var name: String = ""
    var category: String = ""
    var width: Float = 0.0
    var height: Float = 0.0
    var fileSize: Int = 0
    var suffix: String = ""
    var stamp: String = ""
    
    static func creatModel(withImage image: UIImage?) -> DataModel {

        return DataModel(name: "image",
                         category: "_IMAGE",
                          width: Float(image?.size.width ?? 0.0),
                          height: Float(image?.size.height ?? 0.0),
                          fileSize: 0,
                          suffix: "",
                          stamp: "\(Date().timeIntervalSince1970)")
    }
    
    static func creatModel(withFile file: Data?, suffix: String = "") -> DataModel {

        return DataModel(name: "file",
                         category: "_DATA",
                          width: 0.0,
                          height: 0.0,
                          fileSize: file?.count ?? 0,
                          suffix: suffix,
                          stamp: "\(Date().timeIntervalSince1970)")
    }
}

extension Dictionary {
    
    func toJson() -> String? {
        
        do {
            let jsonData = try JSONSerialization.data(withJSONObject: self, options: .prettyPrinted)
            let jsonStr = String(data: jsonData, encoding: .utf8)
            return jsonStr
        } catch let error as NSError {
            return "转换失败:\n\(error.localizedDescription) \n转换终止, 输出如下:\n\(self.description)"
        }
    }
}
